//
//  LSSideBarViewController.m
//  KuaiYi
//
//  Created by ZengFanSheng on 2016/11/13.
//  Copyright © 2016年 HeiMa. All rights reserved.
//

#import "LSSideBarViewController.h"
//#import "LSFunctionListViewController.h"
#import "LSFunctionModel.h"
#import "LSNavigationViewController.h"
#import "HMAdditions.h"
#import <Masonry.h>
#import "LSLoginViewController.h"
#import "LSRegistViewController.h"

@interface LSSideBarViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property(nonatomic,strong) NSArray *groups;

@end

@implementation LSSideBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)loadData
{

    LSFunctionModel *model_00 = [LSFunctionModel modelWithIcon:@"More_LotteryRecommend" Name:@"名医申请" Detail:nil DestinationViewControllerName:@"LSApplyViewController"];
    
    LSFunctionModel *model_01 = [LSFunctionModel modelWithIcon:@"MoreNetease" Name:@"病历管理" Detail:nil DestinationViewControllerName:@"LSIllCaseViewController"];
    
    LSFunctionModel *model_02 = [LSFunctionModel modelWithIcon:@"RedeemCode" Name:@"关注医生" Detail:nil DestinationViewControllerName:@"LSCareViewController"];
    
    NSArray *group_0 = @[model_00,model_01,model_02];
    
    LSFunctionModel *model_10 = [LSFunctionModel modelWithIcon:@"MoreHelp" Name:@"设置与帮助" Detail:nil DestinationViewControllerName:@"LSSettingViewController"];
    
    LSFunctionModel *model_11 = [LSFunctionModel modelWithIcon:@"MoreAbout" Name:@"关于产品" Detail:nil DestinationViewControllerName:@"LSAboutViewController"];
    
    LSFunctionModel *model_12 = [LSFunctionModel modelWithIcon:@"MoreShare" Name:@"分享" Detail:nil DestinationViewControllerName:@"LSShareViewController"];
    
    NSArray *group_1 = @[model_10,model_11,model_12];
    
    self.groups = @[group_0,group_1];
    
}

- (void)setupUI
{
    [super setupUI];
    
    [self loadData];
    
    UIView *headView = [[UIView alloc] init];
    headView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 100) ;
    headView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = headView;
    [self.view addSubview:self.tableView];
    
    
    UIView *loginView = [[UIView alloc] init];
    loginView.frame = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width , 100);
    //loginView.backgroundColor = [UIColor blackColor];
    loginView.backgroundColor = [UIColor hm_colorWithHex:0x20c6c6];
//    [loginView addGestureRecognizer:tapGesture];
    [self.view addSubview:loginView];
    
    
    //距离
    CGFloat margin = 30;
    //添加头像视图
    UIView *photoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 2*margin, 2*margin)];
    photoView.backgroundColor = [UIColor redColor];
    photoView.layer.cornerRadius = margin;
    photoView.center = CGPointMake(0.375 * [UIScreen mainScreen].bounds.size.width, loginView.height/2);
    [loginView addSubview:photoView];
    
    //添加注册按钮
    UIButton *regist = [UIButton buttonWithType:UIButtonTypeCustom];
    [regist setTitle:@"注册" forState:UIControlStateNormal];
    [loginView addSubview:regist];
    [regist mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(photoView.mas_left).offset(-margin);
        make.centerY.equalTo(loginView);
        make.height.mas_equalTo(margin);
    }];
    [regist addTarget:self action:@selector(gotoRegist:) forControlEvents:UIControlEventTouchUpInside];
    
    //添加登录按钮
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    [login setTitle:@"登录" forState:UIControlStateNormal];
    [loginView addSubview:login];
    [login mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(photoView.mas_right).offset(margin);
        make.centerY.equalTo(loginView);
        make.height.mas_equalTo(margin);
    }];
    [login addTarget:self action:@selector(gotoLogin:) forControlEvents:UIControlEventTouchUpInside];
}

/**
 注册
 */
-(void)gotoRegist:(UIButton *)sender{
    ICSDrawerController *drawer = (ICSDrawerController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *controller = [[LSRegistViewController alloc] init];
    controller.title = @"注册";
    UITabBarController *tab = (UITabBarController *)drawer.centerViewController;
    LSNavigationViewController *nav = (LSNavigationViewController *)tab.selectedViewController;
    [nav popToRootViewControllerAnimated:NO];
    [drawer close];
    [nav pushViewController:controller animated:NO];
    
}

/**
 登录
 */
-(void)gotoLogin:(UIButton *)sender{
    ICSDrawerController *drawer = (ICSDrawerController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController *controller = [[LSLoginViewController alloc] init];
    controller.title = @"登录";
    UITabBarController *tab = (UITabBarController *)drawer.centerViewController;
    LSNavigationViewController *nav = (LSNavigationViewController *)tab.selectedViewController;
    [nav popToRootViewControllerAnimated:NO];
    [drawer close];
    [nav pushViewController:controller animated:NO];
    
}

- (UITableView *)tableView{
    
    if (_tableView == nil ) {
        
        CGRect frame = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width*3.0/4.0f , self.view.bounds.size.height);
        _tableView = [[UITableView alloc] initWithFrame: frame style:UITableViewStyleGrouped];;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    
    return _tableView;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSArray *arr = self.groups[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *functionCellIdentifier = @"functionCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:functionCellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:functionCellIdentifier];
    }
    
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic_cell_green_accessory_img"]];;
    
    NSArray *arr = self.groups[indexPath.section];
    LSFunctionModel *model = arr[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:model.icon];
    cell.textLabel.text = model.name;

    return cell;
    
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSArray *arr = self.groups[indexPath.section];
    LSFunctionModel *model = arr[indexPath.row];
    
    Class class = NSClassFromString(model.destinationViewControllerName);
    UIViewController *vc = [[class alloc] init];
    
    NSAssert([vc isKindOfClass:[UIViewController class]], @"class不是UIViewController类型");
    
    
    ICSDrawerController *drawer = (ICSDrawerController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    [drawer close];

    UITabBarController *tab = (UITabBarController *)drawer.centerViewController;
    LSNavigationViewController *nav = (LSNavigationViewController *)tab.selectedViewController;
    
    // [nav popViewControllerAnimated:NO];
    [nav popToRootViewControllerAnimated:NO];
    vc.title = model.name;
    [vc setHidesBottomBarWhenPushed:YES];
    [nav pushViewController:vc animated:NO];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
